const commodityModel = require('../model/commodityModel');
// 条件查询
module.exports.find = async function (searchObj, page) {
    let {
        current,
        pageSize
    } = page;
    // 根据查询条件获取数据总数
    let total = await commodityModel.countDocuments(searchObj);
    // 根据查询条件获取具体的数量
    let data = await commodityModel.find(searchObj).populate({ path: "types" })
        .limit(pageSize).skip((current - 1) * pageSize);
    return {
        current,
        pageSize,
        total,
        rows: data,
    };
}

module.exports.findAll = async function () {
    return await commodityModel.find().populate({ path: "types" });
}

// 增加
module.exports.addCommodity = async function (commodity) {
    return await commodityModel.create(commodity);
}

// 删除
module.exports.delCommodity = async function (id) {
    return await commodityModel.remove({
        _id: id
    });
}
// id查询
module.exports.commodityById = async function (id) {
    return await commodityModel.findById(id).populate({ path: "types" });
}

// 修改
module.exports.updateCommodity = async function (id, commodity) {
    return await commodityModel.updateOne({
        _id: id,
    }, commodity);
}
